import unittest
from ChessBoard import ChessBoard


class TestGetLastTextMove(unittest.TestCase):
    """
    TestGetLastTextMove tests the ChessBoard.getLastTextMove() function.

    Note: I am aware that the test case for success (test_success) uses other
    ChessBoard functions to fully set up the chessboard, and as such the test
    code is running through more code than just it's specific function.

    I think that the lab might have been looking for this code to be tested
    in isolation, but as it's a public facing function which touches other
    parts and members of ChessBoard. I believe this is the correct way to test
    this code. The test shouldn't care _how_ getLastTextMove() gets the last
    text move, only that it does and it's correct!
    """

    def setUp(self):
        self.chessboard = ChessBoard()
        self.chessboard.resetBoard()

    def tearDown(self):
        self.chessboard = None

    def load_local_game(self):
        moves = []
        with open('moves.txt', 'r') as f:
            for line in f:
                moves.extend((line.rstrip()).split(" "))
        return moves

    def test_success(self):
        """Success case, getLastTextMove() returns text of the last move.
        """
        moves = self.load_local_game()

        for move in moves:
            # After adding each move to the chessboard instance, check that
            # the last moves text is the same as the move just added.
            self.chessboard.addTextMove(move)
            self.assertEqual(move, self.chessboard.getLastTextMove())

    def test_no_moves_in_stack(self):
        """Test when no moves are in the stack, returns None.
        """
        self.assertEqual(None, self.chessboard.getLastTextMove())


if __name__ == '__main__':
    unittest.main()
